package Offer2017;

/**
 * 二叉树的深度 及判断是否是平衡二叉树
 * Created by 雷文康 on 2018/1/4.
 */
public class Test39 {
    /**
     * 二叉树的深度
     *
     * @param root
     * @return
     */
    public static int treeDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = treeDepth(root.left);
        int right = treeDepth(root.right);
        return left > right ? (left + 1) : (right + 1);
    }
    /**
     *是否是平衡二叉树
     */
    public static boolean isBalanceBST(TreeNode root){
        if (root == null) {
            return true;
        }
        int left = treeDepth(root.left);
        int right = treeDepth(root.right);
        if (left - right > 1 || left - right < -1) {
            return false;
        }
        return isBalanceBST(root.left) && isBalanceBST(root.right);
    }


}
